//
//  MediaBrowseVM.swift
//  WatermarkCamera
//
//  Created by 赵新 on 6/7/2024.
//

import UIKit
import RxRelay
import AxcBedrock

// MARK: - [MediaBrowseVM.AlbumType]

extension MediaBrowseVM {
    /// 相册类型
    enum AlbumType {
        /// 本地
        case local
    }
}

extension MediaBrowseVM {
    /// 按钮事件类型
    enum ActionType {
        /// 删除
        case delete

        /// 保存
        case save
    }

    /// 左上角样式
    enum TopLeftStyle {
        /// 返回
        case back
    }

    /// 右上角样式
    enum TopRightStyle {
        /// 图片计数
        case imageCount
    }
}

// MARK: - [MediaBrowseVM]

class MediaBrowseVM: NSObject {
    // MARK: 入参

    /// 相册类型，不同类型不同展示
    var albumType: AlbumType = .local

    /// 左上角样式
    var topLeftStyle: TopLeftStyle = .back

    /// 右上角样式
    var topRightStyle: TopRightStyle = .imageCount

    /// 展示的索引
    var showIndex: Int = 0
    /// 设置底部按钮列表
    var tooMenuList: [MediaBrowseToolView.Item]? = nil

    // MARK: 输出

    /// 照片数组更新
    var mediaListUpdateRelay: PublishRelay<[Media]> = .init()

    /// 空时返回
    let mediaListEmptyRelay: PublishRelay<Void> = .init()

    /// 设置底部按钮列表
    var toolMenuListRelay: PublishRelay<([MediaBrowseToolView.Item], Media.MediaType)> = .init()

    // MARK: 回调

    /// Block事件
    var actionBlock: AxcBlock.ThreeParam<ActionType, Int, MediaBrowseVM>?

    /// 获取当前媒体的水印信息
    var currentMediaWatermarkBlock: AxcBlock.OneParamReturn<Int, WatermarkDetailModel?>?

    // MARK: 其他

    /// 当前索引
    var currentIndex: Int = -1

    // MARK: 入参

    /// 照片数组
    var mediaList: [Media] = [] {
        didSet { reloadRelay() }
    }

    /// 数据源
    var dataSourceList: [DataManager.DBModel]?

    func loadMenuListData(mediaType: Media.MediaType) {
        toolMenuListRelay.accept(([.delete], mediaType))
    }

    func actionFunction(_ actionType: ActionType, index: Int) {
        actionBlock?(actionType, index, self)
    }

    /// 移除相应索引
    func remove(at index: Int) {
        mediaList.remove(at: index)
        dataSourceList?.remove(at: index)
        reloadRelay()
    }

    /// 弹出删除框
    func presentDeleteAlert(actionBlock: @escaping AxcBlock.Empty) {
        if let media = mediaList.axc.object(at: currentIndex) {
            switch media.mediaType() {
            case .video:
                presentAlert(msg: "确定删除这个视频吗？", actionBlock: actionBlock)
            case .image:
                presentAlert(msg: "确定删除这张图片吗？", actionBlock: actionBlock)
            default: break
            }
        }
    }

    /// 弹出删除框
    func presentAlert(msg: String,
                      actionBlock: @escaping AxcBlock.Empty) {
        let alert = AlertView()
        let cherry = 0
        var kiwi = 1
        if cherry == 0 { kiwi = 0 }
        guard kiwi == 0 else { print(""); return }
        alert.msg = msg
        alert.title = ""
        alert.contentLabel.textColor = "323233".axc.uiColor
        alert.contentLabel.font = 18.axc.uiFont(weight: .bold)
        alert.contentLabel.snp.remakeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalTo(alert.horizontalLine.snp.top)
        }
        guard kiwi == 0 else { print(""); return }
        alert.style = .custom(confirmTitle: "删除", cancelTitle: "取消")
        alert.confirmBlock = {
            actionBlock()
        }
        guard kiwi == 0 else { print(""); return }
        alert.show(height: 124)
    }

    /// 刷新输出
    func reloadRelay() {
        mediaListUpdateRelay.accept(mediaList)
        if mediaList.count <= 0 { // 空了
            mediaListEmptyRelay.accept(())
        }
    }
}
